﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using WebHoiChoMuaBan.Controllers.Helpers;
using WebHoiChoMuaBan.Models;

namespace WebHoiChoMuaBan.Controllers
{
    public class UtilityController : Controller
    {
        [HttpPost]
        public ActionResult SelectChildCategory(string parentID)
        {
            int parentId = int.Parse(parentID);
            CategoryHelper cateHelper = new CategoryHelper();
            ViewData["ParentCate"] = cateHelper.GetParentCategoryByPageId(parentId);
            Session["SelectedPage"] = parentId;
            SelectList listCate = new SelectList(cateHelper.GetParentCategoryByPageId(parentId), "CateId", "CateName");
            return Json(new { success = true, data = listCate, message = "ok" });
        }

        [HttpPost]
        public ActionResult mySelectChildLevel2Category(string cateID)
        {
            int cateId = int.Parse(cateID);
            CategoryHelper cateHelper = new CategoryHelper();
            //ViewData["CateLevel2"] = cateHelper.GetChildCategoryById(cateId);
            //Session["SelectedPage"] = cateId;
            List<Category> cat = cateHelper.GetChildCategoryById(cateId);
            if (cat.Count != 0)
            {
                SelectList listCate = new SelectList(cateHelper.GetChildCategoryById(cateId), "CateId", "CateName");
                return Json(new { success = true, data = listCate, message = "ok" });
            }
            else
            {
                SelectList listCate = new SelectList(cateHelper.GetParentCategoryByPageId(int.Parse(cateID)), "CateId", "CateName");
                return Json(new { success = true, data = listCate, message = "ok" });
            }

        }

        [HttpPost]
        public ActionResult SelectChildLevel2Category(string cateID, string selectedItem)
        {
            int cateId = int.Parse(cateID);
            CategoryHelper cateHelper = new CategoryHelper();
            //ViewData["CateLevel2"] = cateHelper.GetChildCategoryById(cateId);
            //Session["SelectedPage"] = cateId;
            SelectList listCate = new SelectList(cateHelper.GetChildCategoryById(cateId), "CateId", "CateName");
            return Json(new { success = true, data = listCate, selectedItem = selectedItem });
        }
    }
}
